﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SilverlightChat.Proxy;

namespace SilverlightChat
{
    public partial class MainPage : UserControl
    {
        object syncRoot = new object();
        PubSubClient client = null;

        public MainPage()
        {
            InitializeComponent();
        }

        private void SubscribeButton_Click(object sender, RoutedEventArgs e)
        {
            this.TopicName.IsEnabled = this.SubscribeButton.IsEnabled = false;
            this.PublishText.IsEnabled = true;
            this.Notifications.Text = "";

            this.client = new PubSubClient();
            this.client.NotifyReceived += new EventHandler<NotifyReceivedEventArgs>(client_NotifyReceived);
            this.client.PublishCompleted += new EventHandler<AsyncCompletedEventArgs>(client_PublishCompleted);
            this.client.SubscribeCompleted += new EventHandler<AsyncCompletedEventArgs>(client_SubscribeCompleted);

            this.client.SubscribeAsync(this.TopicName.Text);
        }

        void client_SubscribeCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (!this.IsError(e))
            {
                this.AddNotification("CLIENT ACTION: Subscribed to topic " + this.TopicName.Text);
            }
        }

        void client_PublishCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (!this.IsError(e))
            {
                this.AddNotification("CLIENT ACTION: Published to topic " + this.TopicName.Text + ": " + e.UserState);
            }
        }

        void client_NotifyReceived(object sender, NotifyReceivedEventArgs e)
        {
            if (!this.IsError(e))
            {                
                this.AddNotification("SERVER NOTIFICATION: " + e.request.GetBody<NotificationData>().Content);
            }
        }

        bool IsError(AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                lock (this.syncRoot)
                {
                    this.client.CloseAsync();
                    this.client = null;
                }
                this.AddNotification("ERROR: " + e.Error.Message);
                this.TopicName.IsEnabled = this.SubscribeButton.IsEnabled = true;
                this.PublishText.IsEnabled = false;
            }

            return e.Error != null;
        }

        void AddNotification(string notification)
        {
            this.Notifications.Text += notification + Environment.NewLine;
            this.Notifications.Select(this.Notifications.Text.Length - 1, 0);
        }

        private void PublishText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                lock (this.syncRoot)
                {
                    if (this.client != null)
                    {
                        this.client.PublishAsync(this.TopicName.Text, this.PublishText.Text, this.PublishText.Text);
                        this.PublishText.Text = "";
                    }
                }
            }
        }
    }
}
